<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 头像区域 -->
      <!-- <div class="avatar_box">
        <img src="../../assets/logo.png" alt />
      </div> -->
      <div class="head">
        <h2>欢迎使用实验室管理系统</h2>
      </div>
      <!-- 登陆表单区域 -->
      <el-form
        ref="loginFormRef"
        :rules="loginFormRules"
        :model="form"
        label-width="0px"
        class="login_form"
      >
        <!-- 用户名 -->
        <el-form-item prop="account">
          <el-input v-model="form.account" prefix-icon="iconfont icon-user"  placeholder="请输入用户名"/>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input
            v-model="form.password"
            prefix-icon="iconfont icon-3702mima"
            type="password"
            placeholder="请输入密码"
          />
        </el-form-item>
        <!-- 验证码 -->
        <el-form-item prop="check">
          <el-row :gutter="20">
            <el-col :span="12">
              <el-input
                type="text"
                maxlength="4"
                show-word-limit
                v-model="form.check"
                placeholder="请输入验证码"
                prefix-icon="el-icon-circle-check"
              >
                <i slot="prefix" class="icon iconfont iconicon_lock"></i>
              </el-input>
            </el-col>
            <el-col :span="5" style="height: 20px" id="verificationImg">
              <img :src="this.verificationImg" alt @click="verification" />
            </el-col>
          </el-row>
        </el-form-item>

        <el-row class="link">
          <el-col :span="12" >
            <p @click="register" style="color:white">立即注册</p>
          </el-col>
          <el-col :span="12">
            <p @click="reset" style="color:red">忘记密码?</p> 
          </el-col>
        </el-row>
        <!-- 按钮 -->
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="resetLoginFrom">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { loadImg, logIn } from "@/api/login/login.js";

export default {
  data() {
    return {
      // 登陆表单数据绑定对象
      form: {
          account: "ssdlgf",
          password: "123456fu",
          check: "",
      },
      verificationImg: "",
      //表单验证规则对象
      loginFormRules: {
        //   验证用户名是否合法
        account: [
          { required: true, message: "请输入登录名称", trigger: "blur" },
          { min: 3, max: 10, message: "长度在3到10个字符", trigger: "blur" },
        ],
        //   验证密码是否合法
        password: [
          { required: true, message: "请输入登录密码", trigger: "blur" },
          { min: 5, max: 15, message: "长度在6到15个字符", trigger: "blur" },
        ],
        check: [
          { required: true, message: "请输入验证", trigger: "blur" },
          { min: 4, max: 4, message: "长度在4个字符", trigger: "blur" },
        ],
      },
    };
  },
  mounted() {
    loadImg({})
      .then((response) => {
        console.log(response.data);
        this.verificationImg = window.URL.createObjectURL(response.data);
      })
      .catch((error) => {
        console.log(error.data);
      });
  },
  methods: {
    resetLoginFrom() {
      //   console.log(this);
      this.$refs.loginFormRef.resetFields();
    },
    login() {
      // 路由重定向
      let redirect = this.$route.query.redirect;
      this.$refs.loginFormRef.validate((valid) => {
        if (valid) {
          logIn(this.form, redirect).then((res) => {

            //这里就写成功后的操作
            if (res.code == 2) {
              console.log("账号错误");
            }
            if (res.code == 1) {
              console.log("密码错误");
            }
            if (res.code == 0) {         
              // setToken(res.data);
              sessionStorage.setItem('stuName',res.data.name)
               this.$store.commit('student',res.data);
              localStorage.setItem("data", 'res.data');
              localStorage.setItem("Authorization", res.token);
              // localStorage.clear();
              let url = redirect || "/la";
              this.$router.replace(url);
            }
          });
        }
      });
    },
    verification() {
      loadImg({})
        .then((response) => {
          console.log(response.data);
          this.verificationImg = window.URL.createObjectURL(response.data);
        })
        .catch((error) => {
          console.log(error.data);
        });
    },
    register(){
      this.$router.replace('/register')
    },
    reset(){
      this.$router.replace('/reset')
    }
  },
};
</script>

<style lang="scss" type="text/scss" scoped>
.login_container {
  background: url("../../assets/login.jpg");
  background-size: cover;
  // background-color: #2b4b6b;
  height: 100%;
}
.login_box {
  width: 450px;
  height: 350px;
  background-color: rgba(238, 198, 203, 0.3);
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}
.head {
  font-family: "宋体",sans-serif;
  color: rgb(197, 107, 162);
}
.login_form {
  position: absolute;
  bottom: 0;
  width: 80%;
  padding: 0 20px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}
.link {
  font-size: 1mm;
}
.btns {
  display: flex;
  align-items: center;
  justify-content: center;
 
}
</style>

 